using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

#if UNITY_EDITOR
using UnityEditor;
#endif

[Serializable]
public class GT_NodeWorkView : GT_ViewBase
{
    #region Public Variables

    #endregion

    #region Protected Variables
    Vector2 mousePos;
    int deleteNodeID = 0;
    #endregion

    #region Constructor
    public GT_NodeWorkView() : base("Work View") { }

    #endregion

    #region Main Methods
    public override void UpdateView(Rect editorRect, Rect precentageRect, Event e, GT_NodeGraph curGraph)
    {
        //Update the Base class
        base.UpdateView(editorRect, precentageRect, e, curGraph);

        //Draw Grid
        GT_NodeUtils.DrawNode(viewRect, 60f, 0.15f, Color.white);
        GT_NodeUtils.DrawNode(viewRect, 20f, 0.05f, Color.white);

        //View Background
        GUI.Box(viewRect, viewTitle, viewSkin.GetStyle("ViewBG"));

        //Start drawubg view GUI
        GUILayout.BeginArea(viewRect);
        if (curGraph != null)
        {
            curGraph.UpdateGraphGUI(e, viewRect, viewSkin);
        }
        GUILayout.EndArea();

        //Process our Events
        ProgressEvent(e);
    }

    public override void ProgressEvent(Event e)
    {
        base.ProgressEvent(e);

        if (viewRect.Contains(e.mousePosition))
        {
            //Debug.Log("Inside:" + viewTitle);

            if (e.button == 0)
            {
                if (e.type == EventType.MouseDown)
                {
                    //Debug.Log("Left Clicked in:" + viewTitle);
                }

                if (e.type == EventType.MouseDrag)
                {
                    //Debug.Log("Mouse drag in:" + viewTitle);
                }

                if (e.type == EventType.MouseUp)
                {
                    //Debug.Log("Mouse up in:" + viewTitle);
                }
            }

            if (e.button == 1)
            {
                if (e.type == EventType.MouseDown)
                {
                    mousePos = e.mousePosition;
                    bool overNode = false;
                    deleteNodeID = 0;
                    if (curGraph != null)
                    {
                        if (curGraph.nodes.Count > 0)
                        {
                            for (int i = 0; i < curGraph.nodes.Count; i++)
                            {
                                if (curGraph.nodes[i].nodeRect.Contains(mousePos))
                                {
                                    deleteNodeID = i;
                                    overNode = true;
                                }
                            }
                        }
                    }

                    if (!overNode)
                    {
                        ProcessContextMenu(e, 0);
                    }
                    else
                    {
                        ProcessContextMenu(e, 1);
                    }
                }
            }
        }
    }
    #endregion

    #region Utility Methods

    private void ProcessContextMenu(Event e, int contextID)
    {
        GenericMenu menu = new GenericMenu();

        if (contextID == 0)
        {
            menu.AddItem(new GUIContent("Create Graph"), false, ContextCallback, "0");
            menu.AddItem(new GUIContent("Load Graph"), false, ContextCallback, "1");

            if (curGraph != null)
            {
                menu.AddSeparator("");
                menu.AddItem(new GUIContent("Unload Graph"), false, ContextCallback, "2");

                menu.AddSeparator("");
                menu.AddItem(new GUIContent("Float Node"), false, ContextCallback, "3");
                menu.AddItem(new GUIContent("Add Node"), false, ContextCallback, "4");
            }
        }

        if (contextID == 1)
        {
            if (curGraph != null)
            {
                menu.AddItem(new GUIContent("Delete Node"), false, ContextCallback, "5");
            }
        }

        menu.ShowAsContext();
        e.Use();
    }

    private void ContextCallback(object obj)
    {
        switch (obj.ToString())
        {
            case "0":
                GT_NodePopupWindow.InitNodePopup();
                break;
            case "1":
                GT_NodeUtils.LoadGraph();
                break;
            case "2":
                GT_NodeUtils.UnloadGraph();
                break;
            case "3":
                GT_NodeUtils.CreateNode(curGraph, NodeType.Float, mousePos);
                break;
            case "4":
                GT_NodeUtils.CreateNode(curGraph, NodeType.Add, mousePos);
                break;
            case "5":
                GT_NodeUtils.DeleteNode(deleteNodeID, curGraph);
                break;
            default:
                break;
        }
    }
    #endregion
}
